function calcSum() {
	sum = 0;
	$tr = $('#cart table tbody tr td:nth-child(4)');

	$.each($tr,
			function(i, item)
			{
				sum += parseInt(item.innerHTML);
			});
	$('#cart_sum td:nth-child(2)').text(sum);
}

function addCartHandler()
{
	$('#menulist tbody tr .name').click(
		function() {
			name = $(this).text();
			temp = $('#cart td').filter(function(index) {
				return $(this).text()==name;});

			if(temp.length!=0) return false;

			$cart = $('#cart table');
			menu_id = parseInt($(this).parent().attr('id'));
			$tr = $('<tr></tr>').addClass('cart_item').attr('menu',menu_id);
			$('<td></td>').addClass('name').html($(this).text()).appendTo($tr);
			$('<td></td>').addClass('price').html($(this).next().text()).appendTo($tr);
			$('<td></td>').addClass('quan').html('1').appendTo($tr);
			$('<td></td>').addClass('par_sum').html($(this).next().text()).appendTo($tr);
			$button = $('<td></td>');
			$('<span></span>').addClass('plus').html('+').appendTo($button).click(
				function() {
			   		quan = $(this).parent().siblings('.quan');
			   		par_sum = $(this).parent().siblings('.par_sum');
			   		price = $(this).parent().siblings('.price');

					quan_val = parseInt(quan.text())+1;
					price_val = parseInt(price.text());
					par_sum_val = price_val * quan_val;

					par_sum.text(par_sum_val);
					quan.text(quan_val);
					
					calcSum();
					
				}).css('cursor','pointer');
			$('<span></span>').html(' / ').appendTo($button);
			$('<span></span>').addClass('minus').html('-').appendTo($button).click(
				function() {
			   		quan = $(this).parent().siblings('.quan');
			   		par_sum = $(this).parent().siblings('.par_sum');
			   		price = $(this).parent().siblings('.price');

					quan_val = parseInt(quan.text())-1;
					if(quan_val == 0) { $(this).parent().parent().remove(); }
					else {
						price_val = parseInt(price.text());
						par_sum_val = price_val * quan_val;

						par_sum.text(par_sum_val);
						quan.text(quan_val);
					}

					calcSum();
				}).css('cursor','pointer');

			$button.addClass('button').appendTo($tr);
			$tr.insertBefore('#cart_sum');
			calcSum();
			return false;
		}
	);
}


$(document).ready(function(){
	$('#cart_submit td:nth-child(1) a').click(
		function() {
			$('.cart_item').remove();
			calcSum();
			return false;
		});
			
	$('#cart_submit td:nth-child(2) a').click(
		function() {
			$tr = $('.cart_item');
			m_list = ''
			if($tr.length==0) {
				alert('메뉴를 선택해주세요');
		  		return false;
			}
			for(i=0;i<$tr.length;i++)
			{
				m_list = m_list + $tr.attr('menu') + '|';
				$tr = $tr.next();
			};
			price = $('#cart_sum td:nth-child(2)').text();

			$.ajax({type:"POST",
					url:"/outland/list/order",
					data:{'menu':m_list,'price':price},
					dataType:'text',
					success: function(res)
							 {
							 	if(res == "Success")
								{
									alert('Success');
								}
								else
								{
									alert('Login Plz');
								}
							 },
					error: function(res) { alert('failed'); }
					});
			return false;
		});
	
	addCartHandler();
});

